<?php
defined('IN_TS') or die('Access Denied.');

$giftid = intval($_GET['id']);

//福利信息
$strGift = $new['gift']->find('gift',array(
	'giftid'=>$giftid,
));

if($strGift==''){
	header ( "HTTP/1.1 404 Not Found" );
	header ( "Status: 404 Not Found" );
	$title = '404';
	include pubTemplate ( "404" );
	exit ();
}


if($strGift['isaudit']==1 && $TS_USER['isadmin']==0 || $strGift['isaudit']==1 && $strGift['userid']!=$TS_USER['userid'] && $TS_USER['isadmin']==0){
	tsNotice('商品审核中...');
}

$strGift['title'] = tsTitle($strGift['title']);
$strGift['address'] = tsTitle($strGift['address']);
$strGift['content'] = tsDecode($strGift['content']);
$strGift['coordinate'] = tsTitle($strGift['coordinate']);

$strGift['user'] = aac('user')->getOneUser($strGift['userid']);

$strGift['type'] = $new['gift']->find('gift_type',array(
	
	'typeid'=>$strGift['typeid'],

));

$cover =tsXimg($strGift['photo'],'gift',500,'',$strGift['path']);
// 是否点赞

$isRecommend = $new['gift']->findCount ( 'gift_recommend', array (
	'giftid' => $giftid,
	'userid' => $userid
) );


//giftdo

if($TS_USER['userid']){
	
	$userid = $TS_USER['userid'];
	
	$strGiftUser = $new['gift']->find('gift_users',array(
	
		'giftid'=>$strGift['giftid'],
		'userid'=>$userid,
	
	));

}

$remain = $strGift['quota'] - $strGift['count_userwish'];

//下单的人
$arrDoUsers = $new['gift']->findAll('gift_users',array(
	'giftid'=>$strGift['giftid'],
),'addtime desc');

foreach($arrDoUsers as $key=>$item){
	$arrDoUser[] = $item;
	if(aac('user')->isUser($item['userid'])){
		$arrDoUser[$key]['user'] = aac('user')->getOneUser($item['userid']);
	}
}

//未支付的人数
$countBuy = $new['gift']->findCount('gift_users',array(
	'giftid'=>$strGift['giftid'],
	'status'=>0,
));

//支付未发货的人数
$countPay = $new['gift']->findCount('gift_users',array(
	'giftid'=>$strGift['giftid'],
	'status'=>1,
));

//发货而未收货的人数
$countSent = $new['gift']->findCount('gift_users',array(
	'giftid'=>$strGift['giftid'],
	'status'=>2,
));

//收货的人数
$countReceive = $new['gift']->findCount('gift_users',array(
	'giftid'=>$strGift['giftid'],
	'status'=>3,
));

//收货的人
$arrGiftUsers = $new['gift']->findAll('gift_users',array(
	'giftid'=>$strGift['giftid'],
	'status'=>3,
),'addtime desc');

foreach($arrGiftUsers as $item){
	if(aac('user')->isUser($item['userid'])){
		$arrGiftUser[] = aac('user')->getOneUser($item['userid']);
	}
}


//评论

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;

$url = tsUrl('gift','show',array('id'=>$giftid,'page'=>''));

$lstart = $page*10-10;

$arrComments = $new['gift']->findAll('gift_comment',array(
	'giftid'=>$strGift['giftid'],
),'addtime desc',null,$lstart.',10');

foreach($arrComments as $key=>$item){
	if(aac('user')->isUser($item['userid'])){
		$arrComment[$key] = $item;
		$arrComment[$key]['content'] = tsDecode($item['content']);
		$arrComment[$key]['user'] = aac('user')->getOneUser($item['userid']);
	}
}

$commentNum = $new['gift']->findCount('gift_comment',array(
	'giftid'=>$strGift['giftid'],
));

$pageUrl = pagination($commentNum, 10, $page, $url);

$strTopic = aac('group')->find('group_topic',array(
	'topicid'=>317,
));
$strTopic['content'] = tsDecode($strTopic['content'],$tp);

if(isWeixin()){
	$payurl = tsUrl('gift','weixin',array('orderid'=>$strGiftUser[orderid]));
}else{
	$payurl = tsUrl('gift','pay',array('orderid'=>$strGiftUser[orderid]));
}

switch($strGift['status']){
	case -1:
		$status = '下架';
		break;
	case 0:
		$status = '待售';
		break;
	case 1:
		$status = '售磬';
}

if(!$strGift['cate']){
	$text = '给微澜志愿者的福利：';
}else{
	$text = '为微澜图书馆义卖：';
}

$title = $text.$strGift['title'];
include template("show");